package com.lh.eshop.config;

import java.sql.SQLException;

import javax.sql.DataSource;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;

import com.alibaba.druid.pool.DruidDataSource;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;


@Component
@Data 
@Slf4j
@ConfigurationProperties(prefix="spring.datasource")
public class DruidDataSourceConfig {

	private String type;									
	private String url;											
	private String username;									
	private String password;									
	private String driverClassName;								
	private int initialSize;									
	private int minIdle;										
	private int maxActive;									
	private int maxWait;										
	private int timeBetweenEvictionRunsMillis;				
	private int minEvictableIdleTimeMillis;					
	private String validationQuery;								
	private boolean testWhileIdle;								
	private boolean testOnBorrow;								
	private boolean testOnReturn;								
	private boolean poolPreparedStatements;						
	private int maxPoolPreparedStatementPerConnectionSize;
	private String connectionProperties;		
	private String filters;
	
	
	
	@Bean
	@Primary
	public DataSource dataSource() {
		DruidDataSource dataSource = new DruidDataSource();
		dataSource.setUrl(url);  
        dataSource.setUsername(username);  
        dataSource.setPassword(password);  
        dataSource.setDriverClassName(driverClassName);  
        dataSource.setInitialSize(initialSize);  
        dataSource.setMinIdle(minIdle);  
        dataSource.setMaxActive(maxActive);  
        dataSource.setMaxWait(maxWait);          
        dataSource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);  
        dataSource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);  
        dataSource.setValidationQuery(validationQuery);  
        dataSource.setTestWhileIdle(testWhileIdle);  
        dataSource.setTestOnBorrow(testOnBorrow);  
        dataSource.setTestOnReturn(testOnReturn);  
        dataSource.setPoolPreparedStatements(poolPreparedStatements);  
        dataSource.setMaxPoolPreparedStatementPerConnectionSize(maxPoolPreparedStatementPerConnectionSize);  
        try {
			dataSource.setFilters(filters);
		} catch (SQLException e) {
			e.printStackTrace();
		}
        dataSource.setConnectionProperties(connectionProperties);  
        
        return dataSource;  
	}

	
	
}
